<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/2/24
 * Time: 11:05
 */
class Model_Lottery extends PhalApi_Model_NotORM{
    protected function getTableName($id){
        return 'p_lottery';
    }

  //  public function getPrize($obj){  //用户去中奖
  //      $phone = $obj->phone;
  //      $time = date('Ymd',time());
  //      $ok = mt_rand(500,1000);
  //      c:
  //      if($ok>=500 && $ok<=1000){   //1元券
  //          $sql="select * from p_lottery where FROM_UNIXTIME(time,'%Y%m%d')=$time and gift like '1元' limit 1";
  //          $data =$this ->getORM()-> queryAll($sql,array());
  //
  //          if(intval($data[0]['amount']) == 0 ){   //1元券为空
  //              goto a;
  //          }
  //          if(intval($data[0]['amount']) != 0){   //1元券不为空,奖品减1,
  //             $this->getORM()->where("gift=? and FROM_UNIXTIME(time,'%Y%m%d')=$time",'1元')
  //                        ->update(array('amount' => new NotORM_Literal("amount - 1")));
  //              return 1;
  //          }
  //      }
  //
  //      a:
  //      if($ok>=269 && $ok<=499){   //3元券
  //          $sql="select * from p_lottery where FROM_UNIXTIME(time,'%Y%m%d')=$time and gift like '3元' limit 1";
  //          $data =$this ->getORM()-> queryAll($sql,array());
  //         // echo '<pre>';var_dump($data[0]['amount']);exit;
  //         // if(intval($data[0]['amount']) == 0){   //3元券为空
  //         //     goto b;
  //        //  }
  //          if(intval($data[0]['amount']) != 0){   //3元券不为空,奖品减1,
  //              $this->getORM()->where("gift=? and FROM_UNIXTIME(time,'%Y%m%d')=$time",'3元')
  //                      ->update(array('amount' => new NotORM_Literal("amount - 1")));
  //              return 3;
  //          }else{
  //              goto b;
  //          }
  //      }
  //
  //      b:
  //      if($ok>=169 && $ok<=268){   //5元券
  //          $sql="select * from p_lottery where FROM_UNIXTIME(time,'%Y%m%d')=$time and gift like '5元' limit 1";
  //          $data =$this->getORM() -> queryAll($sql,array());
  //        //  echo '<pre>';var_dump($data[0]['amount']);exit;
  //          //if(intval($data[0]['amount']) == 0){   //5元券为空
  //         //     goto d;
  //         // }
  //          if(intval($data[0]['amount']) != 0){   //5元券不为空,奖品减1,
  //              $this->getORM()->where("gift=? and FROM_UNIXTIME(time,'%Y%m%d')=$time",'5元')
  //                  ->update(array('amount' => new NotORM_Literal("amount - 1")));
  //              return 5;
  //          }else{
  //              goto d;
  //          }
  //      }
  ////echo 'dd';exit;
  //      d:
  //      if($ok>=1 && $ok<=168){   //保单券6
  //          $sql="select * from p_lottery where FROM_UNIXTIME(time,'%Y%m%d')=$time and gift like '6保单' limit 1";
  //          $data =$this ->getORM()-> queryAll($sql,array());
  //         // echo '<pre>';var_dump($data[0]['amount']);exit;
  //          if(intval($data[0]['amount']) == 0){   //保单券为空,去奖品不为空的奖池
  //
  //             $sql="select * from p_lottery where FROM_UNIXTIME(time,'%Y%m%d')=$time and amount not like 0 limit 1";
  //             $data = $this->getORM()->queryAll($sql,array());
  //              if(empty($data)){
  //                  return -2;
  //              }
  //              $gift = $data[0]['gift'];
  //              $num = intval($data[0]['gift']);
  //              //对应的奖品减1
  //              $this->getORM()->where("gift=? and FROM_UNIXTIME(time,'%Y%m%d')=$time",$gift)
  //                  ->update(array('amount' => new NotORM_Literal("amount - 1")));
  //              return $num;   //返回奖品
  //          }
  //
  //          if(intval($data[0]['amount']) != 0){     //保单券不为空,奖品减1,
  //              $this->getORM()->where("gift=? and FROM_UNIXTIME(time,'%Y%m%d')=$time",'6保单')
  //                  ->update(array('amount' => new NotORM_Literal("amount - 1")));
  //              return 6;  //保单
  //          }
  //      }
  //  }
    public function getPrize($obj){  //用户去中奖
        $phone = $obj->phone;
        $time = date('Ymd',time());
        $ok = mt_rand(1,1000);
        if($ok>=500 && $ok<=1000){
            goto c;
        }elseif($ok>=269 && $ok<=499){
            goto a;
        }elseif($ok>=169 && $ok<=268){
            goto b;
        }elseif($ok>=1 && $ok<=168){
            goto d;
        }
        c:
        //1元券
        $sql="select * from p_lottery where FROM_UNIXTIME(time,'%Y%m%d')=$time and gift like '1元' limit 1";
        $data =$this ->getORM()-> queryAll($sql,array());

        // if(!intval($data[0]['amount']) ){   //1元券为空
        //    goto a;
        // }
        if(intval($data[0]['amount']) != 0){   //1元券不为空,奖品减1,
            $this->getORM()->where("gift=? and FROM_UNIXTIME(time,'%Y%m%d')=$time",'1元')
                ->update(array('amount' => new NotORM_Literal("amount - 1")));
            return 1;
        }else{
            goto a;
        }


        a:
        //3元券
        $sql="select * from p_lottery where FROM_UNIXTIME(time,'%Y%m%d')=$time and gift like '3元' limit 1";
        $data =$this ->getORM()-> queryAll($sql,array());
        // echo '<pre>';var_dump($data[0]['amount']);exit;
        // if(intval($data[0]['amount']) == 0){   //3元券为空
        //     goto b;
        //  }
        if(intval($data[0]['amount']) != 0){   //3元券不为空,奖品减1,
            $this->getORM()->where("gift=? and FROM_UNIXTIME(time,'%Y%m%d')=$time",'3元')
                ->update(array('amount' => new NotORM_Literal("amount - 1")));
            return 3;
        }else{
            goto b;
        }

        b:
        //5元券
        $sql="select * from p_lottery where FROM_UNIXTIME(time,'%Y%m%d')=$time and gift like '5元' limit 1";
        $data =$this->getORM() -> queryAll($sql,array());
        //  echo '<pre>';var_dump($data[0]['amount']);exit;
        //if(intval($data[0]['amount']) == 0){   //5元券为空
        //     goto d;
        // }
        if(intval($data[0]['amount']) != 0){   //5元券不为空,奖品减1,
            $this->getORM()->where("gift=? and FROM_UNIXTIME(time,'%Y%m%d')=$time",'5元')
                ->update(array('amount' => new NotORM_Literal("amount - 1")));
            return 5;
        }else{
            goto d;
        }


        d:
        //保单券6
        $sql="select * from p_lottery where FROM_UNIXTIME(time,'%Y%m%d')=$time and gift like '6保单' limit 1";
        $data =$this ->getORM()-> queryAll($sql,array());
        // echo '<pre>';var_dump($data[0]['amount']);exit;
        if(intval($data[0]['amount']) == 0){   //保单券为空,去奖品不为空的奖池

            $sql="select * from p_lottery where FROM_UNIXTIME(time,'%Y%m%d')=$time and amount not like 0 limit 1";
            $data = $this->getORM()->queryAll($sql,array());
            if(empty($data)){
                return -2;
            }
            $gift = $data[0]['gift'];
            $num = intval($data[0]['gift']);
            //对应的奖品减1
            $this->getORM()->where("gift=? and FROM_UNIXTIME(time,'%Y%m%d')=$time",$gift)
                ->update(array('amount' => new NotORM_Literal("amount - 1")));
            return $num;   //返回奖品
        }

        if(intval($data[0]['amount']) != 0){     //保单券不为空,奖品减1,
            $this->getORM()->where("gift=? and FROM_UNIXTIME(time,'%Y%m%d')=$time",'6保单')
                ->update(array('amount' => new NotORM_Literal("amount - 1")));
            return 6;  //保单
        }



    }
  /*
   * 判断奖池是否为空
   */
    public function giftCount(){
        $time = date('Ymd',time());
        $sql1="select amount from p_lottery where FROM_UNIXTIME(time,'%Y%m%d')=$time and gift = '1元'";
        $sql3="select amount from p_lottery where FROM_UNIXTIME(time,'%Y%m%d')=$time and gift = '3元'";
        $sql5="select amount from p_lottery where FROM_UNIXTIME(time,'%Y%m%d')=$time and gift = '5元'";
        $sql6="select amount from p_lottery where FROM_UNIXTIME(time,'%Y%m%d')=$time and gift = '6保单'";
        $info1=$this->getORM()->queryAll($sql1,array());
        $info3=$this->getORM()->queryAll($sql3,array());
        $info5=$this->getORM()->queryAll($sql5,array());
        $info6=$this->getORM()->queryAll($sql6,array());
        $num1=intval($info1[0]['amount']);
        $num3=intval($info3[0]['amount']);
        $num5=intval($info5[0]['amount']);
        $num6=intval($info6[0]['amount']);
        $sum = $num1+$num3+$num5+$num6;
        return $sum;
    }
}